window.onload = function() {
	queryPersonalMessage(GetUrlByParamName("token"))
	user_token = GetUrlByParamName("token")
}
var user_token;
var user_id;
var learnPageNum = 1
var learnPageSize = 12
var learnStart
var learnEnd
var learnTotalPage
var collectionPageNum = 1
var collectionPageSize = 12
var collectionStart
var collectionEnd
var collectionTotalPage
var commentPageNum = 1
var commentPageSize = 8
var commentStart
var commentEnd
var commentTotalPage

function GetUrlByParamName(name)   {    
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");    
	var URL =  decodeURI(window.location.search);    
	var r = URL.substr(1).match(reg);    
	if(r != null) {       //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
		return  decodeURI(r[2]);    
	};    
	return null;  
}

function changeCourseListButtonType(id){
	if(id == "learnCouseListButton"){
		document.getElementById("collectionCouseListButton").classList.remove("active")
		document.getElementById("learnCouseListButton").classList.add("active")
		document.getElementById("collectionPageBox").style.display = "none"
		document.getElementById("learnPageBox").style.display = "block"
	}else if(id == "collectionCouseListButton"){
		document.getElementById("learnCouseListButton").classList.remove("active")
		document.getElementById("collectionCouseListButton").classList.add("active")
		document.getElementById("collectionPageBox").style.display = "block"
		document.getElementById("learnPageBox").style.display = "none"
	}
}

function queryPersonalMessage(a) {
	var data = {
		"token": a,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/personal/getUserAndCount?token=" + a,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			if(r.code == 201) {
				alert("token失效，请重新登录")
				window.location.href="http://localhost/page/LoginPage/Login.html"
			}
			console.log(r)
			user_id = r.data.userId
			//document.getElementById("my-img").src =  r.data.headImg
			document.getElementById("headImg").src = r.data.headImg
			document.getElementById("userName").innerText = r.data.nickName
			document.getElementById("fansCount").innerText = r.data.fansCount
			document.getElementById("attentionCount").innerText = r.data.attentionCount
			getMyCollectionCourseList();
			getMyLearnCourseList();
			getCourseCommentByUserId();
		},
		error: function() {
			alert("error");
		}
	});
}

function getMyLearnCourseList() {
	var data = {
		"pageNum": learnPageNum,
		"pageSize": learnPageSize,
		"userId": user_id,
		"token": user_token,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/course/pcGetMyLearnCouseList?token=" + user_token,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			learnStart = r.data.start
			learnEnd = r.data.end
			learnTotalPage = r.data.totalPage
			document.getElementById("learnCourseCount").innerText = "("+r.data.list.length+")"
			var str = ""
			for(var i = 0; i < r.data.list.length; i++) {
				str += '<div class="box" onclick="gotoCourseDetail('+ r.data.list[i].id+')">'
				str += '<a>'
				str += '<div class="img">'
				str += '<img src="' + r.data.list[i].coverPath + '" alt="' + r.data.list[i].title + '">'
				str += '</div>'
				str += '<!--<div class="courseIdentification"><span>获得证书</span></div>-->'
				str += '<div style="padding-top: 4px;padding-bottom: 4px;padding-left: 7px;padding-right: 7px;text-align: center;">'
				str += '<span style="font-size: 14px;position: relative;top: 10px;">' + r.data.list[i].title + '</span>'
				str += '<div class="AllcourseTime">'
				str += '<span class="label label-primary" style="font-size: 13px;line-height: initial;font-weight: 400;margin-right: 6px;">' + r.data.list[i].level + '</span>'
				str += '<div class="courseTimeText">'
				str += '<span>讲师:' + r.data.list[i].teacherName + '</span>'
				str += '</div>'
				str += '</div>'
				str += '<div class="course-status">已收藏</div>'
				str += '</div>'
				str += '</a>'
				str += '</div>'
			}
			document.getElementById("learnCouseList").innerHTML = str
			createLearnPageBox()
		},
		error: function() {
			alert("error");
		}
	});
}

function getMyCollectionCourseList() {
	var data = {
		"pageNum": collectionPageNum,
		"pageSize": collectionPageSize,
		"userId": user_id,
		"token": user_token,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/course/pcGetMyCollectionCouseList?token=" + user_token,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			collectionStart = r.data.start
			collectionEnd = r.data.end
			collectionTotalPage = r.data.totalPage
			document.getElementById("collectionCourseCount").innerText = "("+r.data.list.length+")"
			var str = ""
			for(var i = 0; i < r.data.list.length; i++) {
				str += '<div class="box" onclick="gotoCourseDetail('+ r.data.list[i].id+')">'
				str += '<a>'
				str += '<div class="img">'
				str += '<img src="' + r.data.list[i].coverPath + '" alt="' + r.data.list[i].title + '">'
				str += '</div>'
				str += '<!--<div class="courseIdentification"><span>获得证书</span></div>-->'
				str += '<div style="padding-top: 4px;padding-bottom: 4px;padding-left: 7px;padding-right: 7px;text-align: center;">'
				str += '<span style="font-size: 14px;position: relative;top: 10px;">' + r.data.list[i].title + '</span>'
				str += '<div class="AllcourseTime">'
				str += '<span class="label label-primary" style="font-size: 13px;line-height: initial;font-weight: 400;margin-right: 6px;">' + r.data.list[i].level + '</span>'
				str += '<div class="courseTimeText">'
				str += '<span>讲师:' + r.data.list[i].teacherName + '</span>'
				str += '</div>'
				str += '</div>'
				str += '<div class="course-status">已收藏</div>'
				str += '</div>'
				str += '</a>'
				str += '</div>'
			}
			document.getElementById("collectionCouseList").innerHTML = str
			createCollectionPageBox()
		},
		error: function() {
			alert("error");
		}
	});
}

function getCourseCommentByUserId() {
	var data = {
		"pageNum": commentPageNum,
		"pageSize": commentPageSize,
		"userId": user_id,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/course/pcGetCourseCommentByUserId?token=" + user_token,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			commentStart = r.data.start
			commentEnd = r.data.end
			commentTotalPage = r.data.totalPage
			document.getElementById("commentCount").innerText = r.data.list.length
			var str = ""
			for(var i = 0; i < r.data.list.length; i++) {
				str += '<div class="commentListPart f-cb">'
				str += '<div style="width: 200px;height: 143.8px;display: flex;float: left;text-align: center;flex-direction:column;color: black;">'
				str += '<h5 style="flex: 1;">来自:</h5>'
				str += '<h5 style="flex: 1;">' + r.data.list[i].courseTitle + '</h5>'
				str += '<h6 style="flex: 1;">' + r.data.list[i].createTime + '</h6>'
				str += '</div>'
				str += '<div class="headslider">'
				str += '<a href="" class="media" target="_blank" title="' + r.data.list[i].userName + '">'
				str += '<img src="' + r.data.list[i].userHeadImage + '" width="40" height="40" />'
				str += '</a>'
				str += '</div>'
				str += '<div class="commentSlider">'
				str += '<div class="tit">'
				str += '<a href="" target="_blank">' + r.data.list[i].userName + '</a>'
				str += '</div>'
				str += '<div class="cnt">'
				str += r.data.list[i].content
				str += '</div>'
				str += '<div class="qa-footer f-cb">'
				str += '<a href="" style="line-height: 36px;color: white;" onclick="delMyComment(' + r.data.list[i].courseId + ',' + r.data.list[i].commentId + ')">删除</a>'
				str += '</div>'
				str += '</div>'
				str += '</div>'
			}
			document.getElementById("sendComment").innerHTML = str
			createCommentPageBox()
		},
		error: function() {
			alert("error");
		}
	});
}

function delMyComment(courseId, commentId) {
	var data = {
		"token": user_token,
		"courseId": courseId,
		"commentId": commentId
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/course/delCourseComment?token=" + user_token,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
		},
		error: function() {
			alert("error");
		}
	});
}

function createLearnPageBox() {
	var learnPageBox = document.getElementById("learnPageBox")
	var option = ""
	option += '<li class="PageUpBox">'
	option += '<a onclick="changeLearnPageBox(1)" class="PageUp">第一页</a>'
	option += '</li>'
	for(var i = learnStart; i <= learnEnd; i++) {
		if(i == learnPageNum) {
			option += '<li class="whichPage">'
			option += '<span class="PageBeSelected" id="' + i + '">' + i + '</span>'
			option += '</li>'
		} else {
			option += '<li class="whichPage">'
			option += '<a onclick="changeLearnPageBox(this.id)" id="' + i + '">' + i + '</a>'
			option += '</li>'
		}
	}
	option += '<li class="PageDownBox">'
	option += '<a onclick="changeLearnPageBox(' + learnTotalPage + ')" class="PageDown">末尾页</a>'
	option += '</li>'
	learnPageBox.innerHTML = option
}

function createCollectionPageBox() {
	var collectionPageBox = document.getElementById("collectionPageBox")
	var option = ""
	option += '<li class="PageUpBox">'
	option += '<a onclick="changeCollectionPageBox(1)" class="PageUp">第一页</a>'
	option += '</li>'
	for(var i = collectionStart; i <= collectionEnd; i++) {
		if(i == collectionPageNum) {
			option += '<li class="whichPage">'
			option += '<span class="PageBeSelected" id="' + i + '">' + i + '</span>'
			option += '</li>'
		} else {
			option += '<li class="whichPage">'
			option += '<a onclick="changeCollectionPageBox(this.id)" id="' + i + '">' + i + '</a>'
			option += '</li>'
		}
	}
	option += '<li class="PageDownBox">'
	option += '<a onclick="changeCollectionPageBox(' + collectionTotalPage + ')" class="PageDown">末尾页</a>'
	option += '</li>'
	collectionPageBox.innerHTML = option
}

function changeCollectionPageBox(id) {
	collectionPageNum = parseInt(id)
	getMyCollectionCourseList()
}
function changeLearnPageBox(id) {
	learnPageNum = parseInt(id)
	getMyLearnCourseList()
}
function createCommentPageBox() {
	var commentPageBox = document.getElementById("commentPageBox")
	var option = ""
	option += '<li class="PageUpBox">'
	option += '<a onclick="changeCommentPageBox(1)" class="PageUp">第一页</a>'
	option += '</li>'
	for(var i = commentStart; i <= commentEnd; i++) {
		if(i == commentPageNum) {
			option += '<li class="whichPage">'
			option += '<span class="PageBeSelected" id="' + i + '">' + i + '</span>'
			option += '</li>'
		} else {
			option += '<li class="whichPage">'
			option += '<a onclick="changeCommentPageBox(this.id)" id="' + i + '">' + i + '</a>'
			option += '</li>'
		}
	}
	option += '<li class="PageDownBox">'
	option += '<a onclick="changeCommentPageBox(' + commentTotalPage + ')" class="PageDown">末尾页</a>'
	option += '</li>'
	commentPageBox.innerHTML = option
}

function changeCommentPageBox(id) {
	commentPageNum = parseInt(id)
	getCourseCommentByUserId()
}

function gotoCourseDetail(courseId){
	window.open("http://localhost/page/coursePage/studyPage.html?courseId="+courseId);
}